home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 4 / The 640 Meg Shareware Studio CD-ROM Volume IV (Data Express)(1994).ISO / clang / 114_01.zip / ED4.BDS < prev    next >
Text File  |  1993-06-01  |  13KB  |  710 lines

  1. /*
  2.  * Screen editor:  window module
  3.  *
  4.  * Source:  ed4.cc
  5.  * Version: August 21, 1981.
  6.  */
  7.  
  8. /* define global constants, variables */
  9.  
  10. #include ed.h
  11. #include bdscio.h
  12. #include ed1.ccc
  13. #include edext.cc
  14.  
  15. /* data global to this module -----
  16.  
  17. char    editbuf[MAXLEN];    the edit buffer
  18. int    editp;            cursor: buffer index
  19. int    editpmax;        length of buffer
  20. int    edcflag;        buffer change flag
  21.  
  22. ----- */
  23.  
  24. /* abort any changes made to current line */
  25.  
  26. edabt()
  27. {
  28.     /* get unchanged line and reset cursor */
  29.     edgetln();
  30.     edredraw();
  31.     edbegin();
  32.     edcflag = NO;
  33. }
  34.  
  35. /* put cursor at beginning of current line */
  36.  
  37. edbegin()
  38. {
  39.     editp = 0;
  40.     outxy(0,outgety());
  41. }
  42.  
  43. /* change editbuf[editp] to c
  44.  * don't make change if line would become to long
  45.  */
  46.  
  47. edchng(c) char c;
  48. {
  49. char oldc;
  50. int k;
  51.     /* if at right margin then insert char */
  52.     if (editp >= editpmax) {
  53.         edins(c);
  54.         return;
  55.     }
  56.     /* change char and print length of line */
  57.     oldc = editbuf[editp];
  58.     editbuf[editp] = c;
  59.     fmtadj(editbuf,editp,editpmax);
  60.     k = fmtlen(editbuf,editpmax);
  61.     if (k > SCRNW1) {
  62.         /* line would become too long */
  63.         /* undo the change */
  64.         editbuf[editp] = oldc;
  65.         fmtadj(editbuf,editp,editpmax);
  66.     }
  67.     else {
  68.         /* set change flag, redraw line */
  69.         edcflag = YES;
  70.         editp++;
  71.         edredraw();
  72.     }
  73. }
  74.  
  75. /* delete the char to left of cursor if it exists */
  76.  
  77. eddel()
  78. {
  79. int k;
  80.     /* just move left one column if past end of line */
  81.     if (edxpos() < outgetx()) {
  82.         outxy(outgetx()-1, outgety());
  83.         return;
  84.     }
  85.     /* do nothing if cursor is at left margin */
  86.     if (editp == 0) {
  87.         return;
  88.     }
  89.     edcflag = YES;
  90.     /* compress buffer (delete char) */
  91.     k = editp;
  92.     while (k < editpmax) {
  93.         editbuf[k-1] = editbuf[k];
  94.         k++;
  95.     }
  96.     /* update pointers, redraw line */
  97.     editp--;
  98.     editpmax--;
  99.     edredraw();
  100. }
  101.  
  102. /* edit the next line.  do not go to end of buffer */
  103.  
  104. eddn()
  105. {
  106. int oldx;
  107.     /* save visual position of cursor */
  108.     oldx = outgetx();
  109.     /* replace current edit line */
  110.     if (edrepl() != OK) {
  111.         return(ERR);
  112.     }
  113.     /* do not go past last non-null line */
  114.     if (bufnrbot()) {
  115.         return(OK);
  116.     }
  117.     /* move down one line in buffer */
  118.     if (bufdn() != OK) {
  119.         return(ERR);
  120.     }
  121.     edgetln();
  122.     /* put cursor as close as possible on this
  123.      * new line to where it was on the old line.
  124.      */
  125.     editp = edscan(oldx);
  126.     /* update screen */
  127.     if (edatbot()) {
  128.         edsup(bufln()-SCRNL2);
  129.         outxy(oldx, SCRNL1);
  130.     }
  131.     else {
  132.         outxy(oldx, outgety()+1);
  133.     }
  134.     return(OK);
  135. }
  136.  
  137. /* put cursor at the end of the current line */
  138.  
  139. edend()
  140. {
  141.     editp = editpmax;
  142.     outxy(edxpos(),outgety());
  143.     
  144.     /* comment out ----- put cursor at end of screen
  145.     outxy(SCRNW1, outgety());
  146.     ----- end comment out */
  147. }
  148.  
  149. /* start editing line n
  150.  * redraw the screen with cursor at position p
  151.  */
  152.  
  153. edgo(n, p) int n, p;
  154. {
  155.     /* replace current line */
  156.     if (edrepl() == ERR) {
  157.         return(ERR);
  158.     }
  159.     /* go to new line */
  160.     if (bufgo(n) == ERR) {
  161.         return(ERR);
  162.     }
  163.     /* prevent going past end of buffer */
  164.     if (bufatbot()) {
  165.         if (bufup() == ERR) {
  166.             return(ERR);
  167.         }
  168.     }
  169.     /* redraw the screen */
  170.     bufout(bufln(),1,SCRNL1);
  171.     edgetln();
  172.     editp = min(p, editpmax);
  173.     outxy(edxpos(), 1);
  174.     return(OK);
  175. }
  176.  
  177. /* insert c into the buffer if possible */
  178.  
  179. edins(c) char c;
  180. {
  181. int k;
  182.  
  183.     /* do nothing if edit buffer is full */
  184.     if (editpmax >= MAXLEN) {
  185.         return;
  186.     }
  187.     /* fill out line if we are past its end */
  188.     if (editp == editpmax && edxpos() < outgetx()) {
  189.         k = outgetx() - edxpos();
  190.         editpmax = editpmax + k;
  191.         while (k-- > 0) {
  192.             editbuf [editp++] = ' ';
  193.         }
  194.         editp = editpmax;
  195.     }
  196.     /* make room for inserted character */
  197.     k = editpmax;
  198.     while (k > editp) {
  199.         editbuf[k] = editbuf[k-1];
  200.         k--;
  201.     }
  202.     /* insert character. update pointers */
  203.     editbuf[editp] = c;
  204.     editp++;
  205.     editpmax++;
  206.     /* recalculate print length of line  */
  207.     fmtadj(editbuf,editp-1,editpmax);
  208.     k = fmtlen(editbuf,editpmax);
  209.     if (k > SCRNW1) {
  210.         /* line would become too long */
  211.         /* delete what we just inserted */
  212.         eddel();
  213.     }
  214.     else {
  215.         /* set change flag, redraw line */
  216.         edcflag = YES;
  217.         edredraw();
  218.     }
  219. }
  220.  
  221. /* join (concatenate) the current line with the one above it */
  222.  
  223. edjoin()
  224. {
  225. int k;
  226.  
  227.     /* do nothing if at top of file */
  228.     if (bufattop()) {
  229.         return;
  230.     }
  231.     /* replace lower line temporarily */
  232.     if (edrepl() != OK) {
  233.         return;
  234.     }
  235.     /* get upper line into buffer */
  236.     if (bufup() != OK) {
  237.         return;
  238.     }
  239.     k = bufgetln(editbuf, MAXLEN);
  240.     /* append lower line to buffer */
  241.     if (bufdn() != OK) {
  242.         return;
  243.     }
  244.     k = k + bufgetln(editbuf+k, MAXLEN-k);
  245.     /* abort if the screen isn't wide enough */
  246.     if (k > SCRNW1) {
  247.         /* bug fix */
  248.         bufgetln(editbuf,MAXLEN);
  249.         return;
  250.     }
  251.     /* replace upper line */
  252.     if (bufup() != OK) {
  253.         return;
  254.     }
  255.     editpmax = k;
  256.     edcflag = YES;
  257.     if (edrepl() != OK) {
  258.         return;
  259.     }
  260.     /* delete the lower line */
  261.     if (bufdn() != OK) {
  262.         return;
  263.     }
  264.     if (bufdel() != OK) {
  265.         return;
  266.     }
  267.     if (bufup() != OK) {
  268.         return;
  269.     }
  270.     /* update the screen */
  271.     if (edattop()) {
  272.          edredraw();
  273.     }
  274.     else {
  275.         k = outgety() - 1;
  276.         bufout(bufln(),k,SCRNL-k);
  277.         outxy(0,k);
  278.         edredraw();
  279.     }
  280. }
  281.  
  282. /* delete chars until end of line or c found */
  283.  
  284. edkill(c) char c;
  285. {
  286. int k,p;
  287.     /* do nothing if at right margin */
  288.     if (editp == editpmax) {
  289.         return;
  290.     }
  291.     edcflag = YES;
  292.     /* count number of deleted chars */
  293.     k = 1;
  294.     while ((editp+k) < editpmax) {
  295.         if (editbuf[editp+k] == c) {
  296.             break;
  297.         }
  298.         else {
  299.             k++;
  300.         }
  301.     }
  302.     /* compress buffer (delete chars) */
  303.     p = editp+k;
  304.     while (p < editpmax) {
  305.         editbuf[p-k] = editbuf[p];
  306.         p++;
  307.     }
  308.     /* update buffer size, redraw line */
  309.     editpmax = editpmax-k;
  310.     edredraw();
  311. }
  312.  
  313. /* move cursor left one column.
  314.  * never move the cursor off the current line.
  315.  */
  316.  
  317. edleft()
  318. {
  319. int k;
  320.  
  321.     /* if past right margin, move left one column */
  322.     if (edxpos() < outgetx()) {
  323.         outxy(max(0, outgetx()-1), outgety());
  324.     }
  325.     /* inside the line.  move left one character */
  326.     else if (editp != 0) {
  327.         editp--;
  328.         outxy(edxpos(),outgety());
  329.     }
  330. }
  331.  
  332. /* insert a new blank line below the current line */
  333.  
  334. ednewdn()
  335. {
  336. int k;
  337.     /* make sure there is a current line and 
  338.      * put the current line back into the buffer.
  339.      */
  340.     if (bufatbot()) {
  341.         if (bufins(editbuf,editpmax) != OK) {
  342.             return;
  343.         }
  344.     }
  345.     else if (edrepl() != OK) {
  346.             return;
  347.     }
  348.     /* move past current line */
  349.     if (bufdn() != OK) {
  350.         return;
  351.     }
  352.     /* insert place holder:  zero length line */
  353.     if (bufins(editbuf,0) != OK) {
  354.         return;
  355.     }
  356.     /* start editing the zero length line */
  357.     edgetln();
  358.     /* update the screen */
  359.     if (edatbot()) {
  360.         /* note: bufln()  >= SCRNL */
  361.         edsup(bufln()-SCRNL2);
  362.         outxy(edxpos(),SCRNL1);
  363.     }
  364.     else {
  365.         k = outgety();
  366.         bufout(bufln(),k+1,SCRNL1-k);
  367.         outxy(edxpos(),k+1);
  368.     }
  369. }
  370.  
  371. /* insert a new blank line above the current line */
  372.  
  373. ednewup()
  374. {
  375. int k;
  376.     /* put current line back in buffer */
  377.     if (edrepl() != OK) {
  378.         return;
  379.     }
  380.     /* insert zero length line at current line */
  381.     if (bufins(editbuf,0) != OK) {
  382.         return;
  383.     }
  384.     /* start editing the zero length line */
  385.     edgetln();
  386.     /* update the screen */
  387.     if (edattop()) {
  388.         edsdn(bufln());
  389.         outxy(edxpos(),1);
  390.     }
  391.     else {
  392.         k = outgety();
  393.         bufout(bufln(),k,SCRNL-k);
  394.         outxy(edxpos(),k);
  395.     }
  396. }
  397.  
  398. /* move cursor right one character.
  399.  * never move the cursor off the current line.
  400.  */
  401.  
  402. edright()
  403. {
  404.     /* if we are outside the line move right one column */
  405.     if (edxpos() < outgetx()) {
  406.         outxy (min(SCRNW1, outgetx()+1), outgety());
  407.     }
  408.     /* if we are inside a tab move to the end of it */
  409.     else if (edxpos() > outgetx()) {
  410.         outxy (edxpos(), outgety());
  411.     }
  412.     /* move right one character if inside line */
  413.     else if (editp < editpmax) {
  414.         editp++;
  415.         outxy(edxpos(),outgety());
  416.     }
  417.     /* else move past end of line */
  418.     else {
  419.         outxy (min(SCRNW1, outgetx()+1), outgety());
  420.     }
  421. }
  422.  
  423. /* split the current line into two parts.
  424.  * scroll the first half of the old line up.
  425.  */
  426.  
  427. edsplit()
  428. {
  429. int p, q;
  430. int k;
  431.  
  432.     /* indicate that edit buffer has been saved */
  433.     edcflag = NO;
  434.     /* replace current line by the first half of line */
  435.     if (bufatbot()) {
  436.         if (bufins(editbuf, editp) != OK) {
  437.             return;
  438.         }
  439.     }
  440.     else {
  441.         if (bufrepl(editbuf, editp) != OK) {
  442.             return;
  443.         }
  444.     }
  445.     /* redraw the first half of the line */
  446.     p = editpmax;
  447.     q = editp;
  448.     editpmax = editp;
  449.     editp = 0;
  450.     edredraw();
  451.     /* move the second half of the line down */
  452.     editp = 0;
  453.     while (q < p) {
  454.         editbuf [editp++] = editbuf [q++];
  455.     }
  456.     editpmax = editp;
  457.     editp = 0;
  458.     /* insert second half of the line below the first */
  459.     if (bufdn() != OK) {
  460.         return;
  461.     }
  462.     if (bufins(editbuf, editpmax) != OK) {
  463.         return;
  464.     }
  465.     /* scroll the screen up and draw the second half */
  466.     if (edatbot()) {
  467.         edsup(bufln()-SCRNL2);
  468.         outxy(1,SCRNL1);
  469.         edredraw();
  470.     }
  471.     else {
  472.         k = outgety();
  473.         bufout(bufln(), k+1, SCRNL1-k);
  474.         outxy(1, k+1);
  475.         edredraw();
  476.     }
  477. }
  478.  
  479. /* move cursor right until end of line or
  480.  * character c found.
  481.  */
  482.  
  483. edsrch(c) char c;
  484. {
  485.     /* do nothing if at right margin */
  486.     if (editp == editpmax) {
  487.         return;
  488.     }
  489.     /* scan for search character */
  490.     editp++;
  491.     while (editp < editpmax) {
  492.         if (editbuf[editp] == c) {
  493.             break;
  494.         }
  495.         else {
  496.             editp++;
  497.         }
  498.     }
  499.     /* reset cursor */
  500.     outxy(edxpos(),outgety());
  501. }
  502.  
  503. /* move cursor up one line if possible */
  504.  
  505. edup()
  506. {
  507. int oldx;
  508.     /* save visual position of cursor */
  509.     oldx = outgetx();
  510.     /* put current line back in buffer */
  511.     if (edrepl() != OK) {
  512.         return(ERR);
  513.     }
  514.     /* done if at top of buffer */
  515.     if (bufattop()) {
  516.         return(OK);
  517.     }
  518.     /* start editing the previous line */
  519.     if (bufup() != OK) {
  520.         return(ERR);
  521.     }
  522.     edgetln();
  523.     /* put cursor on this new line as close as
  524.      * possible to where it was on the old line.
  525.      */
  526.     editp = edscan(oldx);
  527.     /* update screen */
  528.     if (edattop()) {
  529.         edsdn(bufln());
  530.         outxy(oldx, 1);
  531.     }
  532.     else {
  533.         outxy(oldx, outgety()-1);
  534.     }
  535.     return(OK);
  536. }
  537.  
  538. /* delete the current line */
  539.  
  540. edzap()
  541. {
  542. int k;
  543.     /* delete the line in the buffer */
  544.     if (bufdel() != OK) {
  545.         return;
  546.     }
  547.     /* move up one line if now at bottom */
  548.     if (bufatbot()) {
  549.         if (bufup() != OK) {
  550.             return;
  551.         }
  552.         edgetln();
  553.         /* update screen */
  554.         if (edattop()) {
  555.             edredraw();
  556.         }
  557.         else {
  558.             outdelln();
  559.             outxy(0,outgety()-1);
  560.         }
  561.         return;
  562.     }
  563.     /* start editing new line */
  564.     edgetln();
  565.     /* update screen */
  566.     if (edattop()) {
  567.         edsup(bufln());
  568.         outxy(0,1);
  569.     }
  570.     else {
  571.         k = outgety();
  572.         bufout(bufln(),k,SCRNL-k);
  573.         outxy(0,k);
  574.     }
  575. }
  576.  
  577. /* return true if the current edit line is being
  578.  * displayed on the bottom line of the screen.
  579.  */
  580.  
  581. edatbot()
  582. {
  583.     return(outgety() == SCRNL1);
  584. }
  585.  
  586. /* return true if the current edit line is being
  587.  * displayed on the bottom line of the screen.
  588.  */
  589.  
  590. edattop()
  591. {
  592.     return(outgety() == 1);
  593. }
  594.  
  595. /* redraw edit line from index to end of line */
  596. /* reposition cursor */
  597.  
  598. edredraw()
  599. {
  600.     fmtadj(editbuf,0,editpmax);
  601.     fmtsubs(editbuf,max(0,editp-1),editpmax);
  602.     outxy(edxpos(),outgety());
  603. }
  604.  
  605. /* return the x position of the cursor on screen */
  606.  
  607. edxpos()
  608. {
  609.     return(min(SCRNW1,fmtlen(editbuf,editp)));
  610. }
  611.  
  612.  
  613. /* fill edit buffer from current main buffer line.
  614.  * the caller must check to make sure the main
  615.  * buffer is available.
  616.  */
  617.  
  618. edgetln()
  619. {
  620. int k;
  621.     /* put cursor on left margin, reset flag */
  622.     editp = 0;
  623.     edcflag = NO;
  624.     /* get edit line from main buffer */
  625.     k = bufgetln(editbuf,MAXLEN);
  626.     if (k > MAXLEN) {
  627.         error("line truncated");
  628.         editpmax = MAXLEN;
  629.     }
  630.     else {
  631.         editpmax = k;
  632.     }
  633.     fmtadj(editbuf,0,editpmax);
  634. }
  635.  
  636. /* replace current main buffer line by edit buffer.
  637.  * the edit buffer is NOT changed or cleared.
  638.  * return ERR is something goes wrong.
  639.  */
  640.  
  641. edrepl()
  642. {
  643.     /* do nothing if nothing has changed */
  644.     if (edcflag == NO) {
  645.         return(OK);
  646.     }
  647.     /* make sure we don't replace the line twice */
  648.     edcflag = NO;
  649.     /* insert instead of replace if at bottom of file */
  650.     if (bufatbot()) {
  651.         return(bufins(editbuf,editpmax));
  652.     }
  653.     else {
  654.         return(bufrepl(editbuf,editpmax));
  655.     }
  656. }
  657.  
  658. /* set editp to the largest index such that
  659.  * buf[editp] will be printed <= xpos
  660.  */
  661.  
  662. edscan(xpos) int xpos;
  663. {
  664.     editp = 0;
  665.     while (editp < editpmax) {
  666.         if (fmtlen(editbuf,editp) < xpos) {
  667.             editp++;
  668.         }
  669.         else {
  670.             break;
  671.         }
  672.     }
  673.     return(editp);
  674. }
  675.  
  676. /* scroll the screen up.  topline will be new top line */
  677.  
  678. edsup(topline) int topline;
  679. {
  680.     if (outhasup() == YES) {
  681.         /* hardware scroll */
  682.         outsup();
  683.         /* redraw bottom line */
  684.         bufout(topline+SCRNL2,SCRNL1,1);
  685.     }
  686.     else {
  687.         /* redraw whole screen */
  688.         bufout(topline,1,SCRNL1);
  689.     }
  690. }
  691.  
  692. /* scroll screen down.  topline will be new top line */
  693.  
  694. edsdn(topline) int topline;
  695. {
  696.     if (outhasdn() == YES) {
  697.         /* hardware scroll */
  698.         outsdn();
  699.         /* redraw top line */
  700.         bufout(topline,1,1);
  701.     }
  702.     else {
  703.         /* redraw whole screen */
  704.         bufout(topline,1,SCRNL1);
  705.     }
  706. }
  707.  
  708. nsert instead of replace if at bottom of file */
  709.     if (bufatbot()) {
  710.         return(buf